﻿#pragma once
#ifndef XL_XEXPAND_DWEBX_ROUTER_STATIC_H
#define XL_XEXPAND_DWEBX_ROUTER_STATIC_H
#include "pch.h"
#include "oatpp\web\server\HttpRequestHandler.hpp"
#include "module_base.h"
#include "haper/haper.h"



struct WoRequestStaticFileCacheInfo {
	//FILE* file=nullptr;
	bool isSet = false;
	//size_t fileSize=0;
};

class WoRequestStaticHandler :public oatpp::web::server::HttpRequestHandler
{
public:
	WoRequestStaticHandler(const char* path, const char* localPath) {
		m_filePath = localPath;
		if (auto tStr = m_filePath.substr(m_filePath.length()-1, 1);
			(tStr != "/" && tStr != "\\")) {
			m_filePath += "/";
		}
		m_urlPath = path;
		if (auto tStr = m_urlPath.substr(m_urlPath.length()-1, 1);
			(tStr != "/" && tStr != "\\")) {
			m_urlPath += "/";
		}
		CXVector<CXTextA> fList;
		_Wo_Files_FileEnums(localPath, fList, "*.*", false, true);
		for (auto& i : *fList.getVector()) {
			std::string f;
			f += i.get();
			std::string it = f;
			f = localPath;
			if (auto lstr = f.substr(f.length() - 2, 2);
				(lstr == "./" && lstr == ".\\")
				) {
				f = ".";
			}
			else {
				if (auto lstr = f.substr(f.length() - 1, 1);
					(lstr == "/" && lstr == "\\")
					) {
					f = f.substr(0, f.length() - 1);
				}
			}
			m_fileList[it] = WoRequestStaticFileCacheInfo{ true };
			//auto hFile = fopen((f + i.get()).c_str(), "r");
			//if (hFile) {			// Get the file size
			//	fseek(hFile, 0, SEEK_END);
			//	size_t fileSize = ftell(hFile);
			//	rewind(hFile);
			//	m_fileList[it] = WoRequestStaticFileCacheInfo{ hFile , fileSize };
			//}
			//std::cout << f + i.get() <<std::endl;
		}
	};
	~WoRequestStaticHandler() {
		//for (auto&i:m_fileList) {
		//	fclose(i.second.file);
		//}
	};
	std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override;
private:
	std::string m_urlPath;
	std::string m_filePath;
	std::unordered_map<std::string, WoRequestStaticFileCacheInfo> m_fileList;
};


#endif


